package com.erp.erp_mask.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.erp_mask.mapper.TPurapplyMapper;
import com.erp.erp_mask.mapper.TPurchaseMapper;
import com.erp.erp_mask.mapper.TPurcheckMapper;
import com.erp.erp_mask.pojo.TPurchase;
import com.erp.erp_mask.pojo.TPurcheck;
import com.erp.erp_mask.service.TPurcheckService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/pur")
public class TPurcheckController {
    @Resource
    private TPurapplyMapper tPurapplyMapper;
    @Resource
    private TPurcheckMapper tPurcheckMapper;
    @Resource
    private TPurcheckService tPurcheckService;
    @Resource
    private TPurchaseMapper tPurchaseMapper;
    @GetMapping("/audit")
    public ModelAndView index() {
        return new ModelAndView("/pur/purcheck");
    }
    @GetMapping("/searchcheck")
    public Result searchList(String startdate,String overdate,Long pageNo,Long pageSize){
        LambdaQueryWrapper<TPurcheck> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasLength(startdate)&&StringUtils.hasLength(overdate)){
            wrapper.between(TPurcheck::getPurDate,startdate,overdate);
            return Result.success(tPurcheckService.checklist(pageNo,pageSize,wrapper));
        }else{
            return Result.success(tPurcheckService.checklist(pageNo,pageSize,wrapper));
        }
    }

    @PutMapping("/editlist")
    @Transactional
    public Result update(@RequestBody TPurcheck tPurcheck){
        try {
            System.out.println(tPurcheck);
            tPurcheckMapper.editlist(tPurcheck);
            tPurapplyMapper.editlist(tPurcheck);
            tPurchaseMapper.addAll(tPurcheck);
            return Result.success("审核通过！");
        }catch (Exception e){
            return Result.fail("审核失败!");
        }
    }
    @PutMapping("/failedit")
    @Transactional
    public Result failedit(@RequestBody TPurcheck tPurcheck){
        try{
            tPurcheckMapper.editlist(tPurcheck);
            tPurapplyMapper.editlist(tPurcheck);
            return Result.success("修改成功！");
        }catch (Exception e){
            return Result.fail("修改失败!");
        }
    }
}
